﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace KTX
{
    public partial class FrmRoomType : Form
    {
        private SqlConnection con;
        public FrmRoomType()
        {
            InitializeComponent();

            Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var strConnectionString = _config.ConnectionStrings.ConnectionStrings[1].ConnectionString;

            con = new SqlConnection(strConnectionString);
        }

        public void taidulieu() {
            var cmd = new SqlCommand("select * from tb_RoomType", con);
            var tb = new DataTable();
            var result = cmd.ExecuteReader();
            tb.Load(result);
            dataGridView1.DataSource = tb; 
        }
            


        private void btntai_Click(object sender, EventArgs e)
        {
            try
            {
                if (con.State == ConnectionState.Closed) con.Open();
                SqlCommand cmd = new SqlCommand("select *from tb_RoomType", con);
                var tb = new DataTable();
                tb.Load(cmd.ExecuteReader());
                dataGridView1.DataSource = tb;
            }
            catch (Exception erro)
            {
                MessageBox.Show(erro.Message);

            }
        }

        private void btnthem_Click(object sender, EventArgs e)
        {

            if (con.State == ConnectionState.Closed) con.Open();

            if (txtid.Text == "" || txtName.Text == "" || txtPrice.Text == "")
            {
                MessageBox.Show("Không được bỏ trống");
            }
            else
            {

                var str = String.Format("insert into tb_RoomType Values(N'{0}','{1}')", txtName.Text, txtPrice.Text);
                var cmd = new SqlCommand(str, con);
                cmd.ExecuteNonQuery();
                btntai_Click(sender, e); // taidulieu();
            }
        }
        private void btnthoat_Click(object sender, EventArgs e)
        {
            
            Close();
        }
        
        private void btnsua_Click(object sender, EventArgs e)
        {
            try
            {
                if (con.State == ConnectionState.Closed) con.Open();

                var str = String.Format("update tb_RoomType set Name = '{0}',Price={1} where Id = {2}",txtName.Text,txtPrice.Text,txtid.Text);
                var cmd = new SqlCommand(str, con);
                cmd.ExecuteNonQuery();

                taidulieu();
            }
            catch (Exception ex) {

                MessageBox.Show(ex.Message);
            }

           

        }
        int dong;
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            txtid.Text = dataGridView1.Rows[dong].Cells[0].Value.ToString();
            txtName.Text = dataGridView1.Rows[dong].Cells[1].Value.ToString();
            txtPrice.Text = dataGridView1.Rows[dong].Cells[2].Value.ToString();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (con.State == ConnectionState.Closed) con.Open();
                var str = string.Format("delete from tb_RoomType where Id = {0}", txtid.Text);

                var cmd = new SqlCommand(str,con);// sql injection
                //cmd.Parameters.Add("@id", SqlDbType.Int).Value =Convert.ToInt32(txtid.Text);
                if(MessageBox.Show("Bạn có muốn xóa dữ liệu này không?","Thong bao",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
                cmd.ExecuteNonQuery();
                btntai_Click(sender, e);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        

    }
}
